﻿
using System.Xml;
using System;
namespace GeoSketch2.Archaeo
{
    /// <summary>
    /// A record of the survey logbook
    /// </summary>
    public class SurveyLogBookRecord
    {
        #region XMLElementsName
#pragma warning disable 1591
        public const string XML_CurrentBenchmark = "CurrentBenchmark";
        public const string XML_ReferenceBenchmark = "ReferenceBenchmark";
        public const string XML_BackSight = "BackSight";
        public const string XML_ForeSight = "ForeSight";
#pragma warning restore 1591
        #endregion

        #region Fields
        /// <summary>
        /// The current benchmark (ForeSight)
        /// </summary>
        public Benchmark CurrentBenchmark;

        /// <summary>
        /// The reference benchmark (BackSight)
        /// </summary>
        public Benchmark ReferenceBenchmark;

        /// <summary>
        /// The backsight measure
        /// </summary>
        public double BackSight;

        /// <summary>
        /// The foresight measure
        /// </summary>
        public double ForeSight; 
        #endregion

        #region Constructor
        /// <summary>
        /// A record of the survey logbook
        /// </summary>
        public SurveyLogBookRecord()
            : this(null, null, 0, 0)
        {
        }

        /// <summary>
        /// A record of the survey logbook
        /// </summary>
        /// <param name="CurrentBenchmark">The current benchmark (ForeSight)</param>
        /// <param name="ReferenceBenchmark">The reference benchmark (BackSight)</param>
        /// <param name="BackSight">The backsight measure</param>
        /// <param name="ForeSight">The foresight measure</param>
        public SurveyLogBookRecord(Benchmark CurrentBenchmark, Benchmark ReferenceBenchmark, double BackSight, double ForeSight)
        {
            this.CurrentBenchmark = CurrentBenchmark;
            this.ReferenceBenchmark = ReferenceBenchmark;
            this.BackSight = BackSight;
            this.ForeSight = ForeSight;
        }

        /// <summary>
        /// A record of the survey logbook
        /// </summary>
        /// <param name="Xr">The Xmlreader to read from</param>
        /// <param name="Site">The excavation site of the object</param>
        public SurveyLogBookRecord(XmlReader Xr, ArchaeoProject Project)
        {
            Xr.ReadToFollowing(XML_CurrentBenchmark);
            this.CurrentBenchmark = Project.GetBenchmarkFromName(Xr.ReadString());

            Xr.ReadToFollowing(XML_ReferenceBenchmark);
            this.ReferenceBenchmark = Project.GetBenchmarkFromName(Xr.ReadString());

            Xr.ReadToFollowing(XML_BackSight);
            this.BackSight = Convert.ToDouble(Xr.ReadString(), Core.Vars.Setup.GeoSketch.NumberFormat);

            Xr.ReadToFollowing(XML_ForeSight);
            this.ForeSight = Convert.ToDouble(Xr.ReadString(), Core.Vars.Setup.GeoSketch.NumberFormat);

            Xr.ReadEndElement();
        } 
        #endregion

        #region Methods
        /// <summary>
        /// Writes the object to a file
        /// </summary>
        /// <param name="Xw">The XmlWriter to write to</param>
        public void ToFile(XmlWriter Xw)
        {
            Xw.WriteStartElement(this.GetType().FullName);

            #region CurrentBenchmark
            Xw.WriteStartElement(XML_CurrentBenchmark);
            Xw.WriteString(CurrentBenchmark.Name);
            Xw.WriteEndElement();
            #endregion

            #region ReferenceBenchmark
            Xw.WriteStartElement(XML_ReferenceBenchmark);
            Xw.WriteString(ReferenceBenchmark.Name);
            Xw.WriteEndElement();
            #endregion

            #region BackSight
            Xw.WriteStartElement(XML_BackSight);
            Xw.WriteString(BackSight.ToString(Core.Vars.Setup.GeoSketch.NumberFormat));
            Xw.WriteEndElement();
            #endregion

            #region ForeSight
            Xw.WriteStartElement(XML_ForeSight);
            Xw.WriteString(ForeSight.ToString(Core.Vars.Setup.GeoSketch.NumberFormat));
            Xw.WriteEndElement();
            #endregion

            Xw.WriteEndElement();
        } 
        #endregion
    }
}
